package cn.tedu.v1.security;

import cn.tedu.v1.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers(
                        "/pages/prebuilt-pages/default-login.html"
                        , "/pages/prebuilt-pages/default-register.html"
                        , "/assets/**"
                        , "/vendors/**"
                        , "favicon.ico"
                        , "/register"
                )
                .permitAll()
                .anyRequest()
                .authenticated()
                .and().formLogin()
                .loginPage("/pages/prebuilt-pages/default-login.html")
                .loginProcessingUrl("/login")
                .failureUrl("/pages/prebuilt-pages/default-login.html?error")
                .defaultSuccessUrl("/index.html")
                .and().logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/pages/prebuilt-pages/default-login.html?logout");
    }
}
